package com.zjw.zy.coreui.view

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewGroup

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/28 17:29
 */
class TopBarView : BaseViewGroup {

    //固定的左侧按钮
    private val mBackBtn: ImageView by lazy { findViewById(R.id.topbar_back) }
    private val mNavBtn: ImageView by lazy { findViewById(R.id.topbar_nav) }
    private val mLabel: TextView by lazy { findViewById(R.id.topbar_label) }

    //右侧辅助按钮
    private val mRightBtn: ImageView by lazy { findViewById(R.id.topbar_right_btn) }
    private val mRightBtn2: ImageView by lazy { findViewById(R.id.topbar_right_btn2) }
    private val mRightBtn3: ImageView by lazy { findViewById(R.id.topbar_right_btn3) }
    private val mRightBtn4: ImageView by lazy { findViewById(R.id.topbar_right_btn4) }

    private var mListener: TopBarListener? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.top_bar
    }

    override fun onBindListener() {
        mBackBtn.setOnClickListener {
            mListener?.onBackClick()
        }

        mNavBtn.setOnClickListener {
            mListener?.onNavClick()
        }
    }

    override fun onBindValue() {}
    override fun onFindView(rootView: View) {

    }

    fun bindListener(listener: TopBarListener) {
        mListener = listener
    }

    fun hideBackButton() {
        mBackBtn.visibility = View.INVISIBLE
        mBackBtn.isEnabled = false
    }


    fun showBackButton() {
        hideNavButton()
        mBackBtn.visibility = View.VISIBLE
        mBackBtn.isEnabled = true
    }

    fun showNavButton() {
        hideBackButton()
        mNavBtn.visibility = View.VISIBLE
        mNavBtn.isEnabled = true;
    }

    fun hideNavButton() {
        mNavBtn.visibility = View.INVISIBLE
        mNavBtn.isEnabled = false
    }


    fun setLabelText(text: String) {
        mLabel.text = text
    }

    /**
     * 注入图片资源，点击事件
     * */
    fun show1RightButton(image: Drawable, listener: OnClickListener?) {
        mRightBtn.visibility = View.VISIBLE
        mRightBtn.setImageDrawable(image)
        mRightBtn.setOnClickListener(listener)
    }

    /**
     * 注入图片资源，点击事件
     * */
    fun show2RightButton(image: Drawable, listener: OnClickListener?) {
        mRightBtn2.visibility = View.VISIBLE
        mRightBtn2.setImageDrawable(image)
        mRightBtn2.setOnClickListener(listener)
    }

    /**
     * 注入图片资源，点击事件
     * */
    fun show3RightButton(image: Drawable, listener: OnClickListener?) {
        mRightBtn3.visibility = View.VISIBLE
        mRightBtn3.setImageDrawable(image)
        mRightBtn3.setOnClickListener(listener)
    }

    /**
     * 注入图片资源，点击事件
     * */
    fun show4RightButton(image: Drawable, listener: OnClickListener?) {
        mRightBtn4.visibility = View.VISIBLE
        mRightBtn4.setImageDrawable(image)
        mRightBtn4.setOnClickListener(listener)
    }

    /**
     * 注入图片资源，点击事件
     * */
    fun update1RightButton(image: Drawable) {
        mRightBtn.visibility = View.VISIBLE
        mRightBtn.setImageDrawable(image)
    }

    /**
     * 注入图片资源，点击事件
     * */
    fun update2RightButton(image: Drawable) {
        mRightBtn2.visibility = View.VISIBLE
        mRightBtn2.setImageDrawable(image)
    }

    /**
     * 注入图片资源，点击事件
     * */
    fun update3RightButton(image: Drawable) {
        mRightBtn3.visibility = View.VISIBLE
        mRightBtn3.setImageDrawable(image)
    }

    /**
     * 注入图片资源，点击事件
     * */
    fun update4RightButton(image: Drawable) {
        mRightBtn4.visibility = View.VISIBLE
        mRightBtn4.setImageDrawable(image)
    }

    fun hideRightButton() {
        mRightBtn.visibility = View.GONE
        mRightBtn.setOnClickListener(null)
    }


    fun hide2RightButton() {
        mRightBtn2.visibility = View.GONE
        mRightBtn2.setOnClickListener(null)
    }

    fun hide3RightButton() {
        mRightBtn3.visibility = View.GONE
        mRightBtn3.setOnClickListener(null)
    }

    fun hide4RightButton() {
        mRightBtn4.visibility = View.GONE
        mRightBtn4.setOnClickListener(null)
    }

    fun setEnableRightButton(enable: Boolean) {
        mRightBtn.isEnabled = enable
    }


    fun setEnable2RightButton(enable: Boolean) {
        mRightBtn2.isEnabled = enable
    }

    fun setEnable3RightButton(enable: Boolean) {
        mRightBtn3.isEnabled = enable
    }

    fun setEnable4RightButton(enable: Boolean) {
        mRightBtn4.isEnabled = enable
    }

    interface TopBarListener {
        fun onBackClick()

        fun onNavClick()
    }
}